package com.timoon.page;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.markup.head.CssHeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.request.resource.CssResourceReference;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.timoon.app.utils.Constantor;
import com.timoon.component.panel.jlayout.JLayouterPanel;
import com.timoon.component.panel.nav.ResourceMapper;
import com.timoon.sample.dao.model.TUser;
import com.timoon.sample.web.home.HomePanel;
import com.timoon.sample.web.nav.NavigatorPanel;
import com.timoon.sample.web.user.UserService;

/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class HomePage extends WebPage {
	private static final long serialVersionUID = -7317138492598046851L;
	
	@SpringBean
	private UserService userService;
	
	
	public HomePage() {
		// TODO Auto-generated constructor stub
		// 向spring security 获取登陆code，即user.account
//		Object principal = SecurityContextHolder.getContext()
//				.getAuthentication().getPrincipal();
//		String userCode;
//
//		if (principal instanceof UserDetails) {
//			userCode = ((UserDetails) principal).getUsername();
//		} else {
//			userCode = principal.toString();
//		}
		
		String userCode="admin";
		TUser user=this.userService.getUserByAccount(userCode);
		ResourceMapper mapper = userService.getResourceMapper(user);

		/* 清除之前保存在session里高亮的菜单 */
		this.getSession().removeAttribute(
				Constantor.SESSION_HIGH_LIGHT_SPAN);

		this.getSession().setAttribute("current-user", user);
		this.getSession().setAttribute(
				Constantor.SESSION_USER_MAPPER, mapper);
		
		Map<String,Panel> panelMap=new HashMap<String,Panel>();
		panelMap.put(JLayouterPanel.WEST_PANEL, new NavigatorPanel(JLayouterPanel.WEST_PANEL));
		panelMap.put(JLayouterPanel.CENTER_PANEL, new HomePanel(JLayouterPanel.CENTER_PANEL));
		
		JLayouterPanel jLayouterPanel=new JLayouterPanel("container",null,panelMap);
		this.getSession().setAttribute("jlayout-panel", jLayouterPanel);
		this.add(jLayouterPanel);
		
	}
	@Override
	public void renderHead(IHeaderResponse response) {
		// TODO Auto-generated method stub
		super.renderHead(response);
		response.render(CssHeaderItem.forReference(new CssResourceReference(this.getClass(),"resource/kendo.common.min.css")));
		response.render(CssHeaderItem.forReference(new CssResourceReference(this.getClass(),"resource/kendo.default.min.css")));
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
